package com.cleanly.boot.controller;

import com.cleanly.boot.entity.User;
import com.cleanly.boot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wangyupeng
 * @despriction:
 * @ate:Created in 18:31 2019/1/10
 * @modify By:
 */
@Controller
@Slf4j
@RequestMapping(value = "/dev")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/test")
    public List<String> test() {
        return userService.test();
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public List<User> queryInfo() {
        return userService.queryInfo();
    }

    @ResponseBody
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public User getInfo(@RequestBody User user) {
        return userService.getInfo(user);
    }

    @RequestMapping(value = "/adduser", method = RequestMethod.POST)
    public int addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @RequestMapping(value = "/deleteuser", method = RequestMethod.POST)
    public int deleteUser(@RequestBody User user){
        return userService.deleteUser(user);
    }

    @RequestMapping(value = "/updateuser", method = RequestMethod.POST)
    public int updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @RequestMapping(value = "/loginIndex")
    public String loginIndex(){
        return "login";
    }
    @PostMapping("/login")
    public String login(@ModelAttribute("user") User user, HttpServletRequest request){
        String username = request.getParameter("UserName");
        String password = request.getParameter("PassWord");
        user.setUsername(username);
        user.setPassword(password);
        log.info("username:"+username+"、password:"+password);
        Integer num = userService.login(user);
        if(num == null || num == 0){
            return "/users/error";
        }
        return "/users/index";
    }
}